package cn.easybuy.web.backend;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.easybuy.entity.Order;
import cn.easybuy.entity.OrderDetail;
import cn.easybuy.entity.Product;
import cn.easybuy.service.OrderDetailService;
import cn.easybuy.service.OrderService;
import cn.easybuy.service.ProductService;
import cn.easybuy.service.impl.OrderDetailServiceImpl;
import cn.easybuy.service.impl.OrderServiceImpl;
import cn.easybuy.service.impl.ProductServiceImpl;

public class OrderDetailServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		OrderDetailService orService = new OrderDetailServiceImpl();
		OrderService orService2 = new OrderServiceImpl();
		ProductService prService=new ProductServiceImpl();
		PrintWriter out = response.getWriter();
		List<Order> orders = orService2.getOrderList();
		StringBuffer sb = new StringBuffer();
		for (Order order : orders) {
			sb.append("<li style=\"width: 950px;border: 1px solid #c7c7cc;text-align: center;margin:0 auto\"><table cellspacing=\"0\" >");
			sb.append("<tr bgcolor=\"#c7c7cc\">"
					+ "<td width=\"210px\">用户名:"
					+ order.getLoginName()
					+ "</td>"
					+ "<td width=\"280px\">订单号:"
					+ order.getSerialNumber()
					+ "</td>"
					+ "<td width=\"280px\">地址:"
					+ order.getUserAddress()
					+ "</td>"
					+ "<td width=\"180px\">￥"
					+ order.getCost()
					+ "</td></tr>"
					+ "<tr><table cellspacing=\"0\" border=\"1\" style=\"margin: 4px auto 20px\"><tr><td width=\"230px\">商品名称</td><td width=\"230px\">商品图片</td><td width=\"230px\">数量</td><td width=\"230px\">价格</td></tr>");
			List<Object> objects = orService.getDetailsList(order.getId());
			for (int i = 0; i < objects.size(); i++) {
				sb.append("<tr><td>"+((Product) objects.get(i)).getName()+"</td><td>"+ ((Product) objects.get(i)).getFileName()+"</td><td>"
						+ ( (OrderDetail) objects.get(++i)).getQuantity()
						+ "</td><td>"
						+ ( (OrderDetail) objects.get(i)).getCost()
						+ "</td></tr>");
			}
			sb.append("</table></tr></table></li>");
		}
		
		out.print(sb);
		out.flush();
		out.close();
	}
}
